<template>
    <div class="newsImg">
        
        <el-form 
            ref="ruleForm" 
            label-width="100px"
            class="el-form"
            :rules="rules"
            :model="ruleForm">
            

            <div class="left newsImg-item">
                <el-upload
                class="upload-demo"
                action="https://www.yyjpai.com/index/operate/uploadsImg"
                :on-success=handleAvatarSuccess
                name="file"
                :file-list=fileList
                :on-remove="handleRemove"
                list-type="picture">
                <el-button size="small" type="primary">上传图片</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
            </div>


            <div class="right newsImg-item">
                <el-form-item
                    v-for="(domain, index) in ruleForm.domains"
                    :label="'第' +index+'张'"
                    :key="domain.key">
                    <el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain,index)" data-id="">删除</el-button>
                </el-form-item>
            </div>


        </el-form>
       <el-button @click="submitForm('ruleForm')" size="medium" class="addSubimit">提交</el-button>
    </div>
</template>
<script>
import { AddData,getChannel } from '@/assets/js'
export default {
    data(){
        return {
           ruleForm:{
                domains:[]
           },
            url:'',
            title:'',
            fileList:[],
            rules:{}
        }
        
    },
    mounted:function(){
        let imgurl = [],desc=[];
        this.$axios.getAddressJson('information/getAtlas',{id:this.$route.query.id}).then((res)=>{
            if(res.data.code==1){
                res.data.data.map((item,index)=>{
                    
                    imgurl.push({
                        name:index,
                        url:item.imgurl
                    })
                    desc.push({
                        value:item.desc
                    })
                })
                this.ruleForm.domains = desc;
                this.fileList = imgurl;
            }
        })
       
        
    },
    methods:{
        handleRemove(res, file){
            console.log(res)
            this.fileList = file
            console.log(file.indexOf(res))
        },
        removeDomain(e,i){//删除指定标题
            let index = this.ruleForm.domains.indexOf(e);
            if (index !== -1) {
                this.ruleForm.domains.splice(index, 1)
            }
        },
        handleAvatarSuccess(res, file) {//上传成功之后

            this.fileList.push({
                name:'图片',
                url:file.response.data.logo
            });
            console.log(res)
            this.ruleForm.domains.push({
                value:''
            })
            this.url = this.url+'_'+file.response.data.logo;
        },
        submitForm:function(e){//点击提交
            console.log(this.ruleForm.domains)
            console.log(this.fileList)



            if(this.ruleForm.domains.length!=this.fileList.length){
                this.$notify.error({
                    title: '填写信息不一致！',
                });
                return false;
            }


            let A = '';
            let B = '';
            this.ruleForm.domains.map((item,index)=>{//循环输出文字
               if(item.value!=''){
                    item.value==''?item.value=' ':item.value;
                    A = A + '_' + item.value;
               }
            })
            this.fileList.map((item,index)=>{
                if(item.url!=''){
                    B = B + '_' + item.url
                }
            })
            var data = {
                id:this.$route.query.id,
                atlas:B.substr(1),
                desc:A.substr(1)
            }
            
            
            this.$axios.getAddressJson('information/addAtlas',data).then((res)=>{
                console.log(res)
                if(res.data.code==1){
                    
                    this.$notify({
                        title: '添加成功',
                        type: 'success'
                    });
                    setTimeout(()=>{this.$router.go(-1)},500)
                }else{
                    this.$notify.error({
                        title: '添加失败',
                    });
                }
                
            })
        }
    }
}
</script>
<style scoped>
    .newsImg{
        height: 500px;
        overflow-y: scroll;
    }
    .newsImg .el-form{
        display:flex;
    }
    .newsImg .newsImg-item{
        width: 50%;
        flex-basis: 50%;
        display: flex;
        flex-direction: column;
        padding-right: 20px;
        box-sizing: border-box;
    }
    .newsImg .newsImg-item.right{
        padding-top: 40px;
    }
    .newsImg .newsImg-item.right .el-form-item{
        margin-top: 10px;
        margin-bottom: 0;
        height: 92px;
        padding-top: 18px;
        box-sizing: border-box;
    }
    .addSubimit{}
</style>